package com.nstar.countdown.view.main.impl
{
	import com.nstar.base.controls.ImageLoader;
	import com.nstar.base.util.Logger;
	import com.nstar.base.util.StringUtils;
	import com.nstar.countdown.view.itemrenderer.CountDownIcon;
	import com.nstar.countdown.view.main.*;
	import com.nstar.countdown.view.modules.Index;
	import com.nstar.hacocwebfx.view.components.SearchBar;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.containers.VBox;
	import mx.containers.ViewStack;
	import mx.core.Application;
	import mx.events.FlexEvent;
	import mx.events.ItemClickEvent;
	import mx.events.ModuleEvent;
	import mx.managers.PopUpManager;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class MainScreenClass extends VBox
	{
		public var header:Header;
		public var footer:Footer;
		public var roTest:RemoteObject;
		public var vsMain:ViewStack;
		public var loader:Canvas;
		
		public var searchBar:SearchBar;
		
		public static const CREATION_COMPLETE: String = "myCreationComplete";
		
		public var logger:Logger = Logger.getInstance(this);
		
		public var module:IModuleInfo;
		public var activeModule:Object;
		public var selectedItem:Object;
		
		public function MainScreenClass()
		{
			//TODO: implement function
			super();
			this.dispatchEvent( new Event( CREATION_COMPLETE ) );
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			header.addEventListener("itemClick",itemClickHandler);
			popUpSearchBar();
			var index:Index = new Index();
			//加载首页
			//loadModule(header.tbbMenus.dataProvider[0].url);
		}
		
		private function mouseWheelHandler(event:MouseEvent):void{
			searchBar.y =0;
		}
		
		private function popUpSearchBar():void{
			var app = Application.application;
			searchBar = PopUpManager.createPopUp(app,SearchBar) as SearchBar;
			searchBar.x = (app.width - searchBar.width) / 2;
		}
		
		
		//处理Header中的主菜单
		private function itemClickHandler(event:ItemClickEvent):void{
			switch(event.item.type){
				case "CHAT":
					vsMain.selectedIndex = vsMain.numChildren - 1;
					break;
				default:
					vsMain.selectedIndex = 0;
					var url:String = event.item.url;
					if(!StringUtils.isEmpty(url)){
						selectedItem = event.item;
						loadModule(url);
					}
					
			}
		}
		
		//加载模块
		public function loadModule(moduleName:String):void{
			if(StringUtils.isEmpty(moduleName)) return;
			
			module = ModuleManager.getModule(moduleName);
			module.addEventListener(ModuleEvent.READY,moduleHandler);
			module.addEventListener(ModuleEvent.PROGRESS,moduleHandler);
			module.addEventListener(ModuleEvent.UNLOAD,moduleHandler);
			module.addEventListener(ModuleEvent.ERROR,moduleHandler);
			
			module.load();
		}
		
		//模块加载事件处理
		private function moduleHandler(event:ModuleEvent):void{
			switch (event.type){
				case ModuleEvent.READY:
					loader.removeAllChildren();
					activeModule = module.factory.create();
					loader.addChild(activeModule as DisplayObject);
					activeModule.data = this.selectedItem;
					Object(loader.getChildAt(0)).percentWidth = 100;
					//Object(loader.getChildAt(0)).percentHeight = 100;
					ImageLoader.hide();
					break;
				case ModuleEvent.PROGRESS:
					ImageLoader.show(loader);
					break;
				case ModuleEvent.UNLOAD:
					break;
				case ModuleEvent.ERROR:
					selectedItem.error = event.errorText;
					loadError();//转到加载异常界面
					break;
			} 
		}
		
		private function loadError():void{
			loadModule("com/nstar/countdown/view/modules/ModuleLoadError.swf");
		}		
	}
}